<?php
	Import::AppLib("word");
	class BannerController extends Controller {
		public function index(){
			Import::AppLib("word");
			$start = Basic::getVar("start");
			$perpage = Basic::getVar("perpage");
			$search = Basic::getVar("search");
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			$searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
			$sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"id,created,modified";
			$sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
			$banner = $this->loadModel("Banner");
			$bannerRecords = $banner->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
			$totalRecords = $banner->getTotalRecords();
			if ( $start < $totalRecords ){
				$this->set("totalRows",$totalRecords);
			}elseif ( $start >= $totalRecords ) {
				$start = $start-20;
				$bannerRecords = $banner->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
				$this->set("totalRows",$totalRecords);
			}
			$this->set("currentPage",$start);
			$this->set("perpage",$perpage);
			$this->set("search",$search);
			$this->set("searchColumns",$searchColumns);
			$this->set("sortColumn",$sortColumn);
			$this->set("sortCriteria",$sortCriteria);
			$this->set("search",$search);
			$this->set("banners",$bannerRecords);
			$this->set("selectedMenu","BAN");
				
			$this->set( "title" , "SMECA - Article List" );
			$this->setLanguage( "English" );
			$this->renderDefault("display","banner");
		}
		public function view(){
			
		}
		public function add(){
			$this->set( "title" , "SMECA - Banner List" );
			$this->setLanguage("English");
			$this->renderDefault("add","banner");
		}
		public function addpro(){
			$title = Basic::getVar("title");
			$content = Basic::getVar("content");
			$publish = Basic::getVar("published");
			$level = Basic::getVar("level");
			$style = Basic::getVar("style");
			$banner = $this->loadModel("Banner");
			if ( $title && $content && $publish!==false && $level !== false && $style!== false ){
				print_r ( $_REQUEST );	
				$banner -> add( $title , $content , $publish , $level , $style );
				$this->redirect( array("com"=>"banner","action"=>"index"));
			}
			$this->redirect( array("com"=>"banner","action"=>"index"));
		}
		public function edit(){
			$id = Basic::getVar("id");
			if ( $id ){
				$banner = $this->loadModel("Banner");
				$banner_data = $banner->find($id);
				$this->set("banner", $banner_data );
				$banner_data = $banner_data[0];
				$banner->reindexBanner($banner_data->level);
				$indexes = $banner->getBannerIndex($banner_data->level);
				$this->set( "indexes",$indexes);
				$this->set( "title" , "SMECA - Banner List" );
				$this->setLanguage("English");
				$this->renderDefault("edit","banner");
			}else{
				$this->redirect( array("com"=>"banner","action"=>"index"));
			}
		}
		public function editpro(){
			$id = Basic::getVar("id");
			$title = Basic::getVar("title");
			$content = Basic::getVar("content");
			$publish = Basic::getVar("published");
			$level = Basic::getVar("level");
			$style = Basic::getVar("style");
			$index = Basic::getVar("bannerIndex");
			$banner = $this->loadModel("Banner");
			if ( $id!==false && $title && $content && $publish!==false && $level !== false && $style!== false && $index !== false ){
				$banner -> edit($id , $title , $content , $publish , $level , $style , $index );
			}
			$this->redirect( array("com"=>"banner","action"=>"index"));
		}
		/* front */
		public function right(){
				
		}
		public function top(){
				
		}
		public function left(){
				
		}
		public function bottom(){
				
		}
		public function published(){
			$publish = Basic::getVar("publish");
			$id = Basic::getVar("id");
			if ( $id && $publish!==false){
				$banner = $this->loadModel("Banner");
				$banner->published($id,$publish==1?0:1);
				echo 1;
			}else{
				echo 0;
			}
		}
		public function delete(){
			$id = Basic::getVar("id");
			if ( $id ){
				$banner = $this->loadModel("Banner");
				$banner->delete($id);
				echo 1;
			}else{
				echo 0;
			}
		}
	}
?>